/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.standard.webflux.base.handler;

import com.hyts.standard.webflux.base.bean.BaseDTO;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;

/**
 * @project-name:standard-webflux
 * @package-name:com.hyts.standard.webflux.base.handler
 * @author:LiBo/Alex
 * @create-date:2019-10-18 17:56
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class BaseHandler {


    /****************************************************************************************************************************/

    public <T extends BaseDTO> Mono<ServerResponse> render(ServerRequest serverRequest, String viewName, T baseDTO, long delayOperation, HttpMethod... httpMethod){
       return ServerResponse.ok().allow(httpMethod).render(viewName,baseDTO).delayElement(Duration.ofMillis(delayOperation),
                Schedulers.newParallel("BaseHandler",Runtime.getRuntime().availableProcessors()));
    }

    public  <T extends BaseDTO> Mono<ServerResponse> renderGet(ServerRequest serverRequest,String viewName,T baseDTO,long delayOperation){
        return render(serverRequest,viewName,baseDTO,delayOperation,HttpMethod.GET);
    }

    public  <T extends BaseDTO> Mono<ServerResponse> renderGet(ServerRequest serverRequest,String viewName,T baseDTO){
        return render(serverRequest,viewName,baseDTO,0L,HttpMethod.GET);
    }

    public  <T extends BaseDTO> Mono<ServerResponse> renderGet(ServerRequest serverRequest,String viewName){
        return render(serverRequest,viewName,new BaseDTO(),0L,HttpMethod.GET);
    }

    public  <T extends BaseDTO> Mono<ServerResponse> renderPost(ServerRequest serverRequest,String viewName,T baseDTO,long delayOperation){
        return render(serverRequest,viewName,baseDTO,delayOperation,HttpMethod.POST);
    }

    public  <T extends BaseDTO> Mono<ServerResponse> renderPost(ServerRequest serverRequest,String viewName,T baseDTO){
        return render(serverRequest,viewName,baseDTO,0L,HttpMethod.POST);
    }

    public  <T extends BaseDTO> Mono<ServerResponse> renderPost(ServerRequest serverRequest,String viewName){
        return render(serverRequest,viewName,new BaseDTO(),0L,HttpMethod.POST);
    }

    /****************************************************************************************************************************/


    public  Mono<ServerResponse> rest(ServerRequest serverRequest,Object baseDTO,long delayOperation, HttpMethod... httpMethod){
        return ServerResponse.ok().allow(httpMethod).contentType(MediaType.APPLICATION_JSON).body(Mono.just(baseDTO),Object.class)
                .delayElement(Duration.ofMillis(delayOperation),
                Schedulers.newParallel("BaseHandler",Runtime.getRuntime().availableProcessors()));
    }

    public  Mono<ServerResponse> restGet(ServerRequest serverRequest,Object baseDTO,long delayOperation){
        return rest(serverRequest,baseDTO,delayOperation,HttpMethod.GET);
    }

    public  Mono<ServerResponse> restGet(ServerRequest serverRequest,Object baseDTO){
        return rest(serverRequest,baseDTO,0L,HttpMethod.GET);
    }

    public  Mono<ServerResponse> restGet(ServerRequest serverRequest){
        return rest(serverRequest,new BaseDTO(),0L,HttpMethod.GET);
    }

    public  Mono<ServerResponse> restPost(ServerRequest serverRequest,Object baseDTO,long delayOperation){
        return rest(serverRequest,baseDTO,delayOperation,HttpMethod.POST);
    }

    public  Mono<ServerResponse> restPost(ServerRequest serverRequest,Object baseDTO){
        return rest(serverRequest,baseDTO,0L,HttpMethod.POST);
    }

    public  Mono<ServerResponse> restPost(ServerRequest serverRequest){
        return rest(serverRequest,new BaseDTO(),0L,HttpMethod.POST);
    }

}
